﻿var lastShowedSubmenu;
var lastSelectedModule;
var serverMediaFilesFolder;
var currentThemeImagesFolder;
var currentUserCulture;

lastShowedSubmenu = null;
lastSelectedModule = null;
serverMediaFilesFolder = "";
currentThemeImagesFolder = "";
currentUserCulture = "";

$(function () {
    $.ajaxSetup({
        cache: false,
        complete: function () {
            if (typeof Sys != "undefined" && typeof Sys.Mvc != "undefined" &&
                    typeof Sys.Mvc.FormContext != "undefined") {
                Sys.Mvc.FormContext._Application_Load();
            }
        }
    });
    $("#container").css("display", "block");

    if (lastShowedSubmenu != null) {
        selectGroup(lastShowedSubmenu);
    }

    if (lastSelectedModule != null) {
        $("#" + lastSelectedModule + "_Module").addClass("selected");
    }

    $("#MySettings, #CancelNewPassword").live("click", function (event) {
        
        $.ajax(
        {
            url: $Url.resolve("~/MySettings/MySettings"),
            type: "GET",
            cache: false,
            success: function (result) {
                $("#SitePopupContent").html(result);
                
                $.colorbox(
                {
                    width: "550px",
                    height: "630px",
                    inline: true,
                    href: "#SitePopupContent",  
                    onOpen: function () {
                        $("#cboxClose").show();           
                        $("#SitePopupContent").show();

                        if ($("#CubeContainer").length) {
                            $("#CubeContainer").css("display", "none");
                        }

                               
                    },
                     onCleanup: function () {
                        $("#SitePopupContent").empty();
                        $("#SitePopupContent").hide();
                    },
                    onClosed: function () {
                        $("#SitePopupContent").empty();
                        $("#SitePopupContent").hide();
                        if ($("#CubeContainer").length) {
                            $("#CubeContainer").css("display", "inline");
                        }
                    }

                });
                
                Sys.Mvc.FormContext._Application_Load();
                Custom.init();
            },
            complete: function (xhr, status, err) {
                $("#cboxClose").show(); 
                //$("#SitePopupContent").attr("style","border: 1px currentColor;");
                if (status == "error") {
                    handleGlobalError(xhr, status, err);
                }
            }
        });

        return false;
    });

    if ($("#despl-select").length > 0) {
        $("#despl-select")[0].style.display = "none";
    }
    if ($("#despl-replace").length > 0) {
        $("#despl-replace")[0].style.display = "block";
        lis = $("#despl-options")[0].getElementsByTagName("li");
        for (i = 0; i < lis.length; i++) {
            lis[i].onclick = function () {
                $("#despl-select")[0].value = this.getAttribute("rel");
                $("#despl-options")[0].style.display = "none";
                $("#despl-current")[0].innerHTML = this.innerHTML;
            }
            lis[i].onmouseover = function () { this.style.backgroundColor = "#EEE"; }
            lis[i].onmouseout = function () { this.style.backgroundColor = "transparent"; }
        }
        $("#despl-replace")[0].onmouseover = function () {
            $("#despl-options")[0].style.display = "block";
        }
        $("#despl-replace")[0].onmouseout = function () {
            $("#despl-options")[0].style.display = "none";
        }
    }
    $("#despl-contenedor li").live("click", function () {
        $.ajax(
                {
                    url: $Url.resolve("~/Site/UpdateCurrentBusiness"),
                    data: { businessID: $(this).attr("rel") },
                    type: "POST",
                    dataType: "json",
                    cache: false,
                    success: function (result) {

                        if (result.Response) {
                            if (result.LoadHomePage) {
                                window.location.href = $Url.resolve("~/Home");
                            }
                            else {
                                location.reload();
                            }
                        }
                        else {
                            alert("error");
                        }
                    },
                    error: function (xhr, text, err) {
                        if (typeof handleGlobalError != "undefined") {
                            handleGlobalError(xhr, text, err);
                        }
                        else {
                            alert(xhr.responseText);
                        }
                    }
                }
            );
    });

    $("#BussinessUnitList").live("change", function () {
        $.ajax(
        {
            url: $Url.resolve("~/Site/UpdateCurrentBusinessUnit"),
            data: { businessUnitID: $("#BussinessUnitList option:selected").val() },
            type: "POST",
            dataType: "json",
            cache: false,
            success: function (result) {
                $("#openBusinessUnitSelector").text(result);
                $("#toggleBusinessUnitSelector a").toggle();
                $("div#panel").slideUp("slow");
                location.reload();
            },
            error: function (xhr, text, err) {
                if (typeof handleGlobalError != "undefined") {
                    handleGlobalError(xhr, text, err);
                }
                else {
                    alert(xhr.responseText);
                }
            }
        }
    );
    });

    $("#openBusinessUnitSelector").click(function () {
        $("div#panel").slideDown("slow");

    });


    $("#closeBusinessUnitSelector").click(function () {
        $("div#panel").slideUp("slow");
    });

    $("#toggleBusinessUnitSelector a").click(function () {
        $("#toggleBusinessUnitSelector a").toggle();
    });

    $("#divSearch").click(function () {

        var ThisText = encodeURIComponent($("#txtToolSearch").val());
        window.document.location = $Url.resolve("~/Finder?SearchText=" + ThisText);
    });

    $("#txtToolSearch").keypress(function (e) {

        if (e.which == 13) {
            var ThisText = encodeURIComponent($("#txtToolSearch").val());
            window.document.location = $Url.resolve("~/Finder?SearchText=" + ThisText);
        }
    });


});

function selectGroup(groupID) {
    var subMenu = $("#" + groupID + "_GroupMenu");
    subMenu.addClass("selected");
    subMenu.children("a").hide();
    subMenu.children("span").show();
 }

 function deselectGroup(groupID) {
     var subMenu = $("#" + groupID + "_GroupMenu");
     subMenu.removeClass("selected");
     subMenu.children("a").show();
     subMenu.children("span").hide();
  }

function toggleSubmenu(groupID) {
    var controlID;

    deselectGroup(lastShowedSubmenu);
    selectGroup(groupID);

    if (lastShowedSubmenu != null) {
        controlID = "#" + lastShowedSubmenu + "_Group_Modules";
        $(controlID).fadeOut(0, function () {
            showSubmenu(groupID);
        });
    }
    else {
        showSubmenu(groupID);
    }
}

function showSubmenu(groupID) {
    controlID = "#" + groupID + "_Group_Modules";
    lastShowedSubmenu = groupID;
    $(controlID).fadeIn(0);
}

function selectSubMenu(targetController, targetAction, moduleCode) {
    var url;

    if (targetAction.toLowerCase().indexOf("javascript:") > -1) {
        eval(targetAction);
    }
    else {
        $("#" + moduleCode + "_Module").addClass("selected");
        if (lastSelectedModule != null) {
            $("#" + lastSelectedModule + "_Module").removeClass("selected");
        }

        url = $Url.resolve("~/" + targetController);
        if (targetAction != "") {
            url = $Url.resolve("~/" + targetController + "/" + targetAction);
        }

        lastSelectedModule = moduleCode;
        window.location.href = url;
    }
}


function PasswordChangeComplete(content) {
    var result;
    result = content.get_object();

    if (result.State == 2) // Success
    {
        $.ajax(
            {
                url: $Url.resolve("~/MySettings/MySettings"),
                type: "GET",
                cache: false,
                success: function (result) {
                    $("#SitePopupContent").html(result);
                    $.colorbox({ width: "500", height: "620", inline: true, href: "#SitePopupContent" });
                    Sys.Mvc.FormContext._Application_Load();
                    Custom.init();
                },
                complete: function (xhr, status, err) {
                    if (status == "error") {
                        handleGlobalError(xhr, status, err);
                    }
                }
            });
    }
    else if (result.State == 1) // Error
    {
        document.location.href = 'LogOff';
    }
    else {
    }
}

function openCenteredPopup(url, width, height, fixed) {
    var windowOptions;

    if (fixed) {
        windowOptions = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no";
    }
    else {
        windowOptions = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no";
    }

    openCenteredWindow($Url.resolve("~/" + url), height, width, "", windowOptions);        
}

function openCenteredWindow(url, height, width, name, parms) {
    var left = Math.floor((screen.width - width) / 2);
    var top = Math.floor((screen.height - height) / 2);
    var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
    if (parms) { winParms += "," + parms; }
    var win = window.open(url, name, winParms, true);    
    if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
    return win;
}

function preventBackspace(e) {
    var evt = e || window.event;
    if (evt) {
        var keyCode = evt.charCode || evt.keyCode;
        if (keyCode === 8) {
            if (evt.preventDefault) {
                evt.preventDefault();
            } else {
                evt.returnValue = false;
            }
        }
    }
}